package org.wlw.web.service.impl;

import org.wlw.web.anno.Resource;
import org.wlw.web.dao.IUserDao;
import org.wlw.web.models.User;
import org.wlw.web.models.WebException;
import org.wlw.web.service.IUserService;

import java.util.List;

public class UserService extends BaseService implements IUserService {
    private IUserDao userDao;

    @Resource
    public void setUserDao(IUserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public User loadByUsername(String username) {
        return userDao.loadByUsername(username);
    }

    @Override
    public void add(User t) {
        if(loadByUsername(t.getUsername())!=null) {
            throw new WebException("添加的用户已经存在!");
        }
        userDao.add(t);
    }

    @Override
    public void update(User t) {
        userDao.update(t);
    }

    @Override
    public void delete(int id) {
        userDao.delete(id);
    }

    @Override
    public User load(int id) {
        return userDao.load(id);
    }

    @Override
    public List<User> find(User user) {
        return userDao.find(user);
    }

    @Override
    public User login(String username, String password) {
        User tu = this.loadByUsername(username);
        if(tu==null) throw new WebException("用户名和密码不正确.");
        if(!password.equals(tu.getPassword()))
            throw new WebException("用户名和密码不正确。");
        return tu;
    }
}
